<?php

namespace app\common\model;

use app\common\service\StatusService;
use think\Model;


class GoodsSpec extends Model
{

    

    

    // 表名
    protected $name = 'goods_spec';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text',
	    'status_text'
    ];
	public function getStatusTextAttr($value, $data) {
		$value = $value ? :$data['status'];
		$list = StatusService::$goodsSpecStatusList;
		return $list[$value] ?? '';
	}
	
    



    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    
    public function goods()
    {
    	return $this->belongsTo('goods', 'goods_id', 'id', '', 'left')->setEagerlyType(0);
    }
    
	/**
	 * 获取商品规格
	 * @param $goodsId
	 * @param string $status
	 * @return false|\PDOStatement|string|\think\Collection
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
    public function getSpecsByGoodsId($goodsId, $status = '') {
    	$where['goods_id'] = $goodsId;
    	$status && $where['status'] = $status;
    	$specs = $this->where($where)
		    ->select();
    	return $specs;
    }
}
